package com.pucheng.bracelet.ui.fragment.me

import android.content.Intent
import android.os.Bundle
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.ext.REQUEST_CODE_TO_STEP_TAG
import com.pucheng.bracelet.app.ext.REQUEST_CODE_TO_USERINFO
import com.pucheng.bracelet.app.util.CacheUtil
import com.pucheng.bracelet.data.bean.UserCenterBean
import com.pucheng.bracelet.databinding.FragmentMeBinding
import com.pucheng.bracelet.ui.activity.*
import com.pucheng.bracelet.viewmodel.me.MeViewModel
import kotlinx.android.synthetic.main.fragment_me.*
import me.hgj.jetpackmvvm.demo.app.base.BaseFragment

class MeFragment : BaseFragment<MeViewModel, FragmentMeBinding>() {

    var iconRes = intArrayOf(
        R.mipmap.ic_msg_setting,
        R.mipmap.ic_tag_setting,
        R.mipmap.ic_wx_run,
        R.mipmap.ic_feedback,
        R.mipmap.ic_about
    )

    private lateinit var mAdapter: BaseQuickAdapter<UserCenterBean, BaseViewHolder>
    val mData = mutableListOf<UserCenterBean>()
    override fun layoutId() = R.layout.fragment_me
    override fun initView(savedInstanceState: Bundle?) {

        mAdapter = object :
            BaseQuickAdapter<UserCenterBean, BaseViewHolder>(R.layout.item_user_setting, mData) {
            override fun convert(holder: BaseViewHolder, item: UserCenterBean) {
                holder.setImageResource(R.id.iv_setting_icon, item.icon)
                    .setText(R.id.tv_setting_name, item.itemName)
                    .setText(R.id.tv_setting_hint, item.rigthConnect)
            }

        }
        rv_me.adapter = mAdapter
        mAdapter.setOnItemClickListener { _, _, position ->
            when (position) {
                0 -> startActivityForResult(
                    Intent(mActivity, UserInfoActivity::class.java),
                    REQUEST_CODE_TO_USERINFO
                )
                1 -> startActivityForResult(
                    Intent(mActivity, FlagActivity::class.java),
                    REQUEST_CODE_TO_STEP_TAG
                )
                2 -> startActivity(Intent(mActivity, WxMotionActivity::class.java))
                3 -> startActivity(Intent(mActivity, FeedBackActivity::class.java))
                4 -> startActivity(Intent(mActivity, AboutActivity::class.java))
            }
        }
        setDatas()
    }

    fun setDatas() {
        mData.clear()
        val strs = resources.getStringArray(R.array.user_conter)
        for (index in strs.indices) {
            if (index != 1) {
                val bean = UserCenterBean(iconRes[index], strs[index])
                mData.add(bean)
            } else {
                val bean = UserCenterBean(iconRes[index], strs[index], CacheUtil.getStepsFlag())
                mData.add(bean)
            }
        }
        mAdapter.notifyDataSetChanged()
    }

//    override fun createObserver() {
//        super.createObserver()
//        appViewModel.stepsFlag.observeForever { setDatas() }
//    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            REQUEST_CODE_TO_USERINFO -> {
                mViewModel.saveUserInfo()
            }
            REQUEST_CODE_TO_STEP_TAG -> setDatas()
        }
    }
}